package com.click369.crm.controller;

import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.click369.crm.common.ResultVo;
import com.click369.crm.pojo.CDept;
import com.click369.crm.service.DeptService;
import com.github.pagehelper.PageInfo;

@Controller
@RequestMapping("dept")
public class DeptController implements BaseController<CDept>{
	
	@Autowired
	private DeptService deptService;
	
	//添加
	@RequiresPermissions({"adddept"})
	@RequiresRoles({"hrmanager"})
	@RequestMapping("save")
	@ResponseBody
	@Override
	public ResultVo save(CDept t) {
		t.setId(null);
		deptService.save(t);
		return new ResultVo(200,"增加成功");
	}
	
	//根据id删除
	@RequiresPermissions({"deldept"})
	@RequiresRoles({"hrmanager"})
	@RequestMapping("delete")
	@ResponseBody
	@Override
	public ResultVo delete(Integer id) {
		deptService.delete(id);
		return new ResultVo(200,"删除成功");
	}
	
	//修改
	@RequiresPermissions({"hrmanager:updatedept"})
	@RequiresRoles({"hrmanager"})
	@RequestMapping("update")
	@ResponseBody
	@Override
	public ResultVo update(CDept t) {
		Integer id = t.getId();
		if(id == null){
			return new ResultVo(200,"没有id");
		}
		CDept cdept = deptService.getById(id);
		t.setCreateTime(cdept.getCreateTime());
		deptService.update(t);
		return new ResultVo(200,"修改成功");
	}
	
	//查询所有分页
	//@RequestParam(defaultValue="1")如果不传递参数默认是第一页
	@RequiresPermissions({"listdept"})
	@RequestMapping("list")
	@ResponseBody
	@Override     
	public ResultVo select(@RequestParam(defaultValue="1")Integer page, 
			@RequestParam(defaultValue="3")Integer rows, CDept t) {
		PageInfo info = deptService.select(page, rows, t);
		return new ResultVo(info.getTotal(),info.getList());
	}
	
	//根据id查询
	@RequestMapping("getById")
	@RequiresRoles({"hrmanager"})
	@ResponseBody
	@Override
	public ResultVo getById(Integer id) {
		CDept byid = deptService.getById(id);
		return new ResultVo(byid);
	}
}
